<template>
  <el-dialog
    v-dialogDrag
    :title="title"
    :visible.sync="visible"
    width="800px"
    append-to-body
  >
    <el-form ref="form" :model="form" label-width="auto" :rules="rules">      

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="案例名" prop="name" class="person">
            <el-input
              v-model="form.name"
              placeholder="案例名"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="发布医生" prop="userId" class="person">
            <el-input
              v-if="type === 'edit'"
              :value="form.doctorName"
              readonly
            ></el-input>
            <el-select v-else v-model="form.userId" placeholder="请选择">
              <el-option
                v-for="item in doctors"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio :label="'0'">草稿</el-radio>
              <el-radio :label="'1'">正式</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="标签列表" prop="labelList">
            <div class="label-list">
              <el-tag
                v-for="item in form.labelList"
                :key="item"
                closable
                @close="handleClose(tag)"
              >
                {{ item }}
              </el-tag>
              <el-input
                v-if="inputVisible"
                v-model="inputValue"
                ref="saveTagInput"
                size="small"
                class="input-new-tag"
                @keyup.enter.native="handleInputConfirm"
                @blur="handleInputConfirm"
              ></el-input>
              <el-button v-else class="button-new-tag" size="small" @click="showInput">+</el-button>
            </div>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="患者基本信息" prop="disease">
            <el-input
              v-model="form.disease"
              placeholder="患者基本信息"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="病史描述" prop="change">
            <el-input
              v-model="form.change"
              placeholder="病史描述"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row  :gutter="20">
        <el-col :span="12">
          <el-form-item label="案例处理体会" prop="diagnosisTime">
            <el-input
              v-model="form.diagnosisTime"
              placeholder="案例处理体会"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="影像检查" prop="hospital">
            <el-input
              v-model="form.hospital"
              placeholder="影像检查"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row  :gutter="20">
        <el-col :span="24">
          <el-form-item label="疗效评估" prop="treatment">
            <el-input
              v-model="form.treatment"
              placeholder="疗效评估"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row  :gutter="20">
        <el-col :span="24">
          <el-form-item label="治疗方案" prop="programme">
            <el-input
              v-model="form.programme"
              placeholder="治疗方案"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>

  </el-dialog>
</template>

<script>
import BindDialog from '@/components/bindDialog/index.vue'
import { dialogFrame } from './frameData'
import { modifyCase, addCase } from '@/api/case'
import { getDoctorList } from '@/api/doctor';

const markForm = () => {
  return {
    name: "",
    userId: "",
    disease: "",
    change: "",
    diagnosisTime: "",
    hospital: "",
    treatment: "",
    programme: "",
    status: '0',
    labelList: [],
    doctorName: '',
    doctorId: '',
  }
}

export default {
  name: "",
  components: {
    BindDialog
  },
  data() {
    return {
      rules: {
        name: [{ required: true, message: '案例名是必填项', trigger: 'blur' }],
        userId: [{ required: true, message: '发布医生是必填项', trigger: 'change' }],
        status: [{ required: true, message: '状态是必填项', trigger: 'change' }],
        labelList: [{ required: true, message: '标签列表是必填项', trigger: 'blur' }],
        disease: [{ required: true, message: '患者基本信息是必填项', trigger: 'blur' }],
        change: [{ required: true, message: '病史描述是必填项', trigger: 'blur' }],
        diagnosisTime: [{ required: true, message: '案例处理体会是必填项', trigger: 'blur' }],
        hospital: [{ required: true, message: '影像检查是必填项', trigger: 'blur' }],
        treatment: [{ required: true, message: '疗效评估是必填项', trigger: 'blur' }],
        programme: [{ required: true, message: '治疗方案是必填项', trigger: 'blur' }],
      },
      doctors: [],
      title: "",
      type: "",
      config: {
        edit: {
          func: modifyCase,
          title: "编辑"
        },
        new: {
          func: addCase,
          title: "新增"
        }
      },
      form: markForm(),
      visible: false,
      inputValue: "",
      inputVisible: false,
      dialogFrame,
      query: '',
      loading: false,
      options: []
    };
  },
  methods: {
    queryDoctorAsync() {
      getDoctorList({
          pageNum: 1,
          pageSize: 500,
      }).then(res => {
        if(res.rows && res.rows.length >= 0){
          this.doctors = res.rows.filter(item => item.name).map(item => {
            return {
              label: item.name,
              value: item.id
            }
          })
        }
      })
    },

    handleClose(tag) {
      this.form.labelList.splice(this.form.labelList.indexOf(tag), 1);
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput?.$refs?.input.focus();
      });
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.form.labelList.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = '';
    },
    remoteMethod(query) {
      if (query !== '') {
        this.loading = true;
        getDoctorList({
          pageNum: 1,
          pageSize: 100,
          name: query,
        }).then(res => {
          if(res.rows && res.rows.length > 0){
            this.loading = false;
            this.options = res.rows;
          }
        })
      } else {
        this.options = [];
      }
    },

    showDialog(row) {
      this.reset();
      this.queryDoctorAsync()
      const isEdit = !!row;
      this.visible = true;
      this.type = isEdit ? "edit" : "new";
      if (isEdit) {
        this.form = Object.assign(this.form, row);
        if(row.userVo){
          this.form.doctorName = row.userVo.name;
        }
       
      }
      this.title = this.config[this.type].title;
    },

    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.visible = false;
          const data = { ...this.form }

          if(this.type === "new"){
            delete data.id;
          }

          data.labels = JSON.stringify(data.labelList);

          delete data.commentNum
          delete data.labelList
          delete data.createBy
          delete data.createTime
          delete data.num
          delete data.taskId
          delete data.topicalUserList
          delete data.userVo
          delete data.doctorName

          this.config[this.type]?.func(data).then(() => {
            this.$modal.msgSuccess("提交成功");
            this.visible = false;
            this.$emit("getTableList");
          });
        } else {
          return false;
        }
      });
    },

    cancel() {
      this.visible = false;
    },

    // 表单重置
    reset() {
      this.form = markForm();
      this.$forceUpdate();
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    },

  },
};
</script>
<style scoped lang="scss">
.label-list{
  height: 36px;
  display: flex;
  align-items: center;

  .el-tag{
    margin-right: 10px;
  }

  .button-new-tag{
    height: 28px;
    padding: 0 20px;
  }

  .input-new-tag{
    width: 100px;
    height: 28px;
    display: flex;

    ::v-deep .el-input__inner {
      width: 100%;
      height: 100%;
    }
  }
}
.person{
  position: relative;

  .el-icon-user{
    position: absolute;
    top: 10px;
    right: 10px;
    cursor: pointer;
  }

  .cover{
    width: 100%;
    height: 100%;
    position:absolute;
    top: 0;
    left: 0;
    z-index: 1;
    cursor: pointer;

    &:hover {
      .el-input__inner{
        border-color: #C0C4CC;
      }
    }
  }
}
</style>
